
//-------------------------------------------------------
// Header Files
//-------------------------------------------------------
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h> // for EXIT_FAILURE
#include "storage.h" // Defines max len for inputs
#include "utils.h"
#include <sys/time.h>

//-------------------------------------------------------
// Constants
//-------------------------------------------------------
#define LOGGING 2
//According to stuff.txt
#define MAXNUMOFKEYS 693
#define MAXb 2503281
enum {Connect = 1, Authenticate = 2, Get = 3, Set = 4, Disconnect = 5, Exit = 6, Query = 7};
		int notMain = 1; // Only parent process have this as 1. 
		int firstTime =1; 
//-------------------------------------------------------
// Main
//-------------------------------------------------------
/**
 * Main function initializes the client
 * and deals with parsing user input
*/
int main(int argc, char *argv[])
{
        // Set up time for logging
    char fileName[MAX_STRTYPE_SIZE];
    time_t basetime;
    struct tm * conTime;
    basetime = time(NULL);
    conTime = localtime(&basetime);
    strftime(fileName, sizeof(fileName), "Client-%Y-%m-%d-%H-%M-%S.log",conTime);
    int tempT = 0; // change in time


        int logChoice = LOGGING;
     switch(logChoice)
    {
	case 0: // Disable Logging
	    fileClient = NULL;
	    break;
	case 1: // Log to stdout
	    fileClient = stdout;
	    break;
	case 2: // Log to File
	    // Append file for appending an update
	    fileClient = fopen (fileName,"a+");
	    if (fileClient == NULL)
	    {
		printf("Error opening file.\n");
		fclose(fileClient);
		exit(EXIT_FAILURE);
	    }
	    break;
    }

        // Variables (add 1 for '/0' character)
    int choice = 0;

    int i = 0;
    int status;
    void *conn;
    struct storage_record r; // the structure type for the record value
    // start executing 
char hostname[MAX_HOST_LEN+1] = "localhost"; 
int portNum = 1376;
    conn = storage_connect(hostname, portNum);
    if(!conn)
    {
	 sprintf(message, "Cannot connect to server @ %s:%d. Error code: %d.\n", hostname, portNum, errno);
	 logger(fileClient, message);
	 return -1;
    }
    else
    {
	printf("			1SUCCESS\n");
    }
char username[MAX_USERNAME_LEN+1] = "admin";
char password[MAX_ENC_PASSWORD_LEN+1] = "dog4sale";

    status = storage_auth(username, password, conn); if(status == 0) printf("			2 SUCCESS\n"); else printf("2 status is %d\n", status); 


//Query performance eval
//Peforms MAXNUMOFKEYS times of query functions
int keysMax = MAXNUMOFKEYS;
char** keys;
int keysMax_for_query = MAXb;
char table[MAX_TABLE_LEN+1] = "census";
keys = (char**) malloc(sizeof(char**) * keysMax);	// Initialize an array to store each column name
for(i = 0; i < keysMax; i++)
{
    keys[i] = (char*) malloc(sizeof(char*) * (MAX_COLNAME_LEN+1));
}

    struct timeval start_time, end_time;
    gettimeofday(&start_time, NULL);
for(i = 0; i < keysMax; i++)
{
    keys[i] = (char*) malloc(sizeof(char*) * (MAX_COLNAME_LEN+1));
}

char predicates[500];
int     predicate_col_b;
for(i = 0; i < keysMax_for_query; i++)
{
       predicate_col_b = rand()%(keysMax_for_query);
    sprintf(predicates, "b > %d", predicate_col_b);
       status = storage_query(table, predicates, keys, keysMax, conn); if(status != -1) printf("			5 SUCCESS with value %d\n", status); else printf("5 status is %d\n", status); 
       for(i = 0; i < status; i++)
       {
	printf("%s\n",keys[i]);
       }

}
        
   for(i = 0; i < keysMax; i++)	//	Free colName
      {
	free(keys[i]);
       }
       free(keys);
       
        gettimeofday(&end_time, NULL);
        tempT = ((end_time.tv_sec - start_time.tv_sec)*1000000L+end_time.tv_usec) - start_time.tv_usec;
        sprintf(message, "%d: Query Time in micros\n", tempT);
        logger(fileClient, message);
        sprintf(message, "storage_Query:succesful");
        printf("%s", message);
        logger(fileClient, message);

    status = storage_disconnect(conn); if(status == 0) printf("			1000 SUCCESS\n"); else printf("1000 status is %d\n", status); 
//-----------------
while(1); 
//-----------------
    // Exit the client
    return 0;
}